﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;
using System.Collections;
using System.Collections.ObjectModel;

namespace DownloadInterface
{
    [Serializable()]
    public class DownloadManager : IDownloadManage
    {
        public ObservableCollection<DownloadTask> tasks;

        public Hashtable taskDownloadActions;//<DownloadTask, DownloadAction> 关闭时需要保存

        [field:NonSerialized]public event DownloadCompletedEventHandler DownloadCompleted;

        [field: NonSerialized]public event DownloadErrorEventHandler DownloadErrorEvent;

        public DownloadManager()
        {
            tasks = new ObservableCollection<DownloadTask>();
            taskDownloadActions = new Hashtable();
        }

        public bool startDownload(DownloadTask downloadTask)
        {
            try
            {
                DownloadAction da = new DownloadAction(downloadTask, DownloadCompleted, DownloadErrorEvent);
                if(da.startDownload() == true) //启动下载成功
                {
                    taskDownloadActions.Add(downloadTask, da);
                    tasks.Add(downloadTask);
                }
                return true;
            }
            catch (DownloadInterface.PathInvalidException)
            {
                if (DownloadErrorEvent != null)
                {
                    DownloadErrorEvent(DownloadError.ErrorCode.DOWNLOAD_FILENAME_INVALIDE);
                }
                Console.WriteLine("文件路径无效");
                return false;
            }
            catch (DownloadInterface.FileNameInvalidException)
            {
                if (DownloadErrorEvent != null)
                {
                    DownloadErrorEvent(DownloadError.ErrorCode.DOWNLOAD_FILENAME_INVALIDE);
                }
                Console.WriteLine("文件名无效");
                return false;
            }
            catch(System.UriFormatException)
            {
                if (DownloadErrorEvent != null)
                {
                    DownloadErrorEvent(DownloadError.ErrorCode.DOWNLOAD_URL_INVALIDE);
                }
                Console.WriteLine("URI格式不正确");
                return false;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

       
        public ObservableCollection<DownloadTask> getDownloadList()
        {
            return tasks;
        }

        public void operateDownload(DownloadTask task, DownloadTaskOperation operation)
        {
            try
            {
                DownloadAction da = taskDownloadActions[task] as DownloadAction;
                if (da == null && operation != DownloadTaskOperation.DOWNLOAD_NEW)
                {
                    return;
                }
                switch (operation)
                {
                    case DownloadTaskOperation.DOWNLOAD_PAUSE:
                        {
                            da.pauseDownload();
                            break;
                        }
                    case DownloadTaskOperation.DOWNLOAD_CONTINUE:
                        {
                            da.completeEvent = DownloadCompleted;  //要加？
                            da.startDownload();
                            break;
                        }
                    case DownloadTaskOperation.DOWNLOAD_NEW:
                        {
                            startDownload(task);
                            break;
                        }
                    case DownloadTaskOperation.DOWNLOAD_CANCEL:
                        {
                            da.cancelDownload();
                            taskDownloadActions.Remove(task);
                            tasks.Remove(task);
                            break;
                        }
                }
            }
            catch (System.Net.WebException ex)
            {
                DownloadErrorEvent(DownloadError.ErrorCode.DOWNLOAD_NETWORK_DISCONNECTED);
            }
            
        }

        public void SaveState()
        {
            //throw new NotImplementedException();
            foreach (DictionaryEntry de in taskDownloadActions)
            {
                ((DownloadAction)de.Value).pauseDownload();
            }
        }
    }
}
